@ECHO OFF

goto script_main



rem ====================================================================
rem ====================================================================
:process_file

set PROG_NAME=%1

echo	.
echo	.
echo	----------------------------------
echo	           %PROG_NAME%
echo	----------------------------------
echo	.
echo	.

readpe.exe -i -f csv ../../build/%PROG_NAME% > tmp_out.txt
if errorlevel 1 (
	exit /B 1
)
for /F "tokens=1,2,3" %%i in (tmp_out.txt) do call :process %%i %%j %%k
del tmp_out.txt
goto :EOF

:process
set VAR1=%1
set VAR2=%2
set VAR3=%3

IF [%VAR2%]==[] (
	echo %VAR1%
	echo %VAR1% >> tmp_list.txt
)
goto :EOF

rem ====================================================================
rem ====================================================================


:script_main

rem		Process release files
call :process_file, CadPlayG.exe
call :process_file, cpg.dll

rem		Process debug files
call :process_file, CadPlayG_debug.exe
call :process_file, cpg__debug.dll

rem		Generate the file with dependencies
echo	Now sorting and removing duplicates...
for /f "tokens=* delims= " %%a in (tmp_list.txt) do (
	if "%%a" == "KERNEL32.dll " (
		echo skipping %%a > nul
	) else (
	if "%%a" == "cpg.dll " (
		echo skipping %%a > nul
	) else (
	if "%%a" == "cpg__debug.dll " (
		echo skipping %%a > nul
	) else (
		find /i "%%a" New.txt > nul
		if errorlevel 1 echo %%a>>New.txt
	)
	)
	)	
)
del tmp_list.txt
sort New.txt > ../../build/dependencies.txt
del New.txt


@ECHO ON
EXIT /B 0